#ifndef SPARK_LLM_SERVER_H 
#define SPARK_LLM_SERVER_H

#include <curl/curl.h>
#include <string>
#include <memory>
#include <functional>

#include "llm_base.h"

#define API_URL "https://spark-api-open.xf-yun.com/v1/chat/completions"
#define API_PASSWORD "hISidBdKPTibdwqwwVfj:csOcWdIzxPyzbRPVTnUM"

#define MEM_GPT_BUF_INIT_SIZE 1024
#define MEM_GPT_BUF_BLOCK_SIZE 1024
#define API_AUTHOR_STR ("Authorization: Bearer" API_PASSWORD)

class SparkServer : public LlmBase{
public:
    static std::string buffer;
    static bool is_post_done;

    static std::function<void(const std::string&)> user_stream_callback;
    static std::function<void(const std::string&)> user_callback;

public:
    SparkServer();
    ~SparkServer();

    bool init();

    void setUserCallback(std::function<void(const std::string&)> func) {
        user_callback = func;
    }

    void setUserStreamCallback(std::function<void(const std::string&)> func) {
        user_stream_callback = func;
    }

    bool is_post_finished(void) {
        return is_post_done;
    }

    bool postRequest(const std::string& question);

private:
    CURL* curl;
    struct curl_slist* headers;

    static void parsering_response(std::string &reponse);

    static size_t write_callback(void* ptr, size_t size, size_t nmemb, char* arg);
};

#endif // LLM_SERVER_H
